package com.lx.springmvcproject2.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.lx.springmvcproject2.entity.Customer;
import com.lx.springmvcproject2.entity.ResultBody;
import com.lx.springmvcproject2.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lx
 * @since 2021-11-03
 */
@Api(tags = "客户接口")
@RestController
@RequestMapping("/customer")
public class CustomerController  {
    @Autowired
    private CustomerService customerService;
    @ApiOperation(value ="新增一个客户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customer", value = "客户类", required = true, dataType = "Customer"),
    })
    @PostMapping("/add")
    public ResultBody add(@RequestBody Customer customer)
    {
        boolean col=customerService.save(customer);
        return ResultBody.success();
    }
    @ApiOperation(value ="删除一个客户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "客户id", required = true, dataType = "Integer"),
    })
    @DeleteMapping("/delete")
    public ResultBody del(@RequestParam("id") int id)
    {
       boolean col= customerService.removeById(id);
       return ResultBody.success();
    }

    @ApiOperation(value ="查找客户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tels",value = "条件",required = false,dataType = "List<String>"),
            @ApiImplicitParam(name = "name", value = "客户名", required = true, dataType = "String"),
    })
    @GetMapping("/list")
    public ResultBody list(@RequestParam(value = "tels",required = false)List<String> tels,@RequestParam(value = "name",required = false,defaultValue = "-1")String name)
    {
        List<Customer> customerList=new ArrayList<>();
        if(tels==null)
        {
            customerList=customerService.getCunstomerList(null,name);
        }
        else
        {
            customerList=customerService.getCunstomerList(tels,name);
        }
        return ResultBody.success(customerList);
    }

    @ApiOperation(value ="修改客户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customer", value = "客户类", required = true, dataType = "Customer"),
    })
    @PutMapping("/update")
    public ResultBody update(@RequestParam("customer")Customer customer)
    {
        boolean col=customerService.updateById(customer);
        return ResultBody.success();
    }

}
